#include "FastCluster.h"
#include <cmath>
#include <sstream>
#include <algorithm>
namespace cluster
{

//1. 这个参数为聚类匹配簇限制距离
constexpr double ConstDist = 2.0;

//2. 测试数据结构
struct Data_t
{
    Data_t(uint8_t t, double v) : type(t), x(v)
    {

    }

    uint8_t type;
    double x;
};
using vecData_t = std::vector<std::shared_ptr<Data_t>>;

//3. 遍历聚类
class TestCluster : public FastCluster
{
public:
    TestCluster() : FastCluster()
    {

    }

    ~TestCluster()
    {

    }

private:
    //重载匹配函数
    bool isMatch(const std::shared_ptr<ClusterObject_t> &a, const std::shared_ptr<ClusterObject_t> &b) const
    {
        auto obja = static_cast<Data_t*>(a->obj);
        auto objb = static_cast<Data_t*>(b->obj);
        return obja->type == objb->type && std::fabs(obja->x - objb->x) < ConstDist;
    }
};

//4. 网格聚类
class TestCluster2D : public FastCluster
{
public:
    TestCluster2D() : FastCluster()
    {

    }

    ~TestCluster2D()
    {

    }

private:
    //重载匹配函数
    bool isMatch(const std::shared_ptr<ClusterObject_t> &a, const std::shared_ptr<ClusterObject_t> &b) const
    {
        auto obja = static_cast<Data_t*>(a->obj);
        auto objb = static_cast<Data_t*>(b->obj);
        return obja->type == objb->type;
    }
};

//5. 打印显示
void print(const vecClusterRes_t &vecRes)
{
    std::vector<Data_t*> vecData;
    std::ostringstream ss;
    ss << "聚类簇:" << vecRes.size() << "\n";
    for (auto &res : vecRes)
    {
        //1.
        vecData.clear();
        for (auto &obj : res->vecObject)
        {
            vecData.push_back(static_cast<Data_t*>(obj->obj));
        }

        //2.
        std::sort(vecData.begin(), vecData.end(), [](Data_t *a, Data_t *b) -> bool
        {
            return a->x < b->x;
        });

        //3.
        ss << "type = " << (uint16_t)vecData.front()->type << " : ";
        for (std::size_t i = 0; i != vecData.size(); ++i)
        {
            ss << vecData[i]->x << "  ";
        }
        ss << "\n";
    }

    std::cout << ss.str() << "\n";
}

}